package com.xiongya.sort;

import com.xiongya.entity.ListNode;

/**
 * @Description: 力扣学习
 * @author: xiongzhilong
 * @date: 2021/1/3 2:35 下午
 */
public class LeetCodeStudy {

    /**
     * 分隔链表
     * @param head 链表头节点
     * @param x     分隔数
     */
    public static ListNode partition (ListNode head, int x) {

        //定义所有小于x的头节点
        ListNode small = new ListNode(0);
        //备份该头节点
        ListNode smallNode = small;
        //定义所有大于或等于x的头节点
        ListNode large = new ListNode(0);
        //备份该头节点
        ListNode largeNode = large;
        //循环遍历每个节点
        while (head != null) {
            if (head.val < x) {
                small.next = head;
                small = small.next;
            } else {
                large.next = head;
                large = large.next;
            }
            head = head.next;
        }
        large.next = null;
        //连接两个链表
        small.next = largeNode.next;
        //largeNode.next = null;
        return smallNode.next;
    }

    public static int fib (int n) {
        if (n < 2) {
            return n;
        } else {
            return fib(n-1) + fib(n-2);
        }
    }

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        ListNode node1 = new ListNode(4);
        ListNode node2 = new ListNode(3);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(5);
        ListNode node5 = new ListNode(6);

        node.next = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = null;

        ListNode head = partition(node, 3);
        System.out.println(111);
    }
}
